package com.song.tradition.page.history

import android.content.Intent
import android.net.Uri
import android.util.Log
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyHorizontalGrid
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.blur
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.compose.ui.zIndex
import androidx.constraintlayout.compose.ConstraintLayout
import androidx.navigation.NavController
import androidx.navigation.compose.rememberNavController
import com.song.tradition.R
import com.song.tradition.entity.WebViewData
import com.song.tradition.entity.courseList
import com.song.tradition.entity.grassData
import com.song.tradition.entity.storyData
import com.song.tradition.page.components.CategoryItem
import com.song.tradition.page.components.LinearStoryItemCard
import com.song.tradition.page.components.ScreenRoute
import com.song.tradition.page.components.TitleWithMoreTab
import com.song.tradition.page.components.VideoCard
import com.song.tradition.page.components.WebViewCard
import com.song.tradition.ui.theme.customBgColor
import com.song.tradition.ui.theme.customCardBgColor
import com.song.tradition.ui.theme.customTitleColor
import com.song.tradition.ui.theme.text100

@Composable
fun HistoryScreen(
    navController: NavController
) {

    val context = LocalContext.current

    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(customBgColor)
            .verticalScroll(rememberScrollState())
    ) {

        Spacer(modifier = Modifier.height(8.dp))

        Surface(
            modifier = Modifier.padding(horizontal = 16.dp),
            shape = RoundedCornerShape(10.dp),
            shadowElevation = 2.dp,
        ) {
            VideoCard(
                url = "http://sns-video-bd.xhscdn.com/85ca8a1d6917b42ffd7ca4093de596144b8d1811_v1_ln",
                context = context
            )
        }

        Spacer(modifier = Modifier.height(8.dp))

        Box(
            modifier = Modifier
                .padding(horizontal = 16.dp)
                .fillMaxWidth(),
        ) {
            Box(modifier = Modifier.fillMaxWidth()) {

                ConstraintLayout(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(top = 8.dp)
                        .background(Color.Transparent)

                ) {
                    val (avatar, boxBg) = createRefs()
                    Card(modifier = Modifier
                        .constrainAs(avatar) {
                            top.linkTo(parent.top)
                            bottom.linkTo(parent.bottom, 16.dp)
                            start.linkTo(parent.start, 16.dp)
                        }
                        .width(120.dp)
                        .height(130.dp)
                        .zIndex(1f),
                        shape = RoundedCornerShape(15.dp),
                        elevation = CardDefaults.cardElevation(2.dp),
                        border = BorderStroke(1.dp, color = customCardBgColor)
                    ) {
                        Image(
                            painterResource(id = R.drawable.history01),
                            contentDescription = "",
                            contentScale = ContentScale.FillHeight,
                            modifier = Modifier.fillMaxSize()

                        )
                    }
                    Box(modifier = Modifier
                        .constrainAs(boxBg) {
                            bottom.linkTo(parent.bottom)
                            start.linkTo(parent.start, 16.dp)
                            end.linkTo(parent.end, 16.dp)
                        }
                        .height(120.dp)
                        .fillMaxWidth()
                        .clip(RoundedCornerShape(10.dp))
                        .clickable {
                            navController.navigate(ScreenRoute.OriginScreen.route)
                        }
                        .background(color = customCardBgColor)) {


                        Row() {
                            Box(
                                modifier = Modifier
                                    .width(136.dp)
                                    .fillMaxHeight()
                            )

                            Column(
                                modifier = Modifier
                                    .padding(8.dp)
                                    .fillMaxHeight(),
                                verticalArrangement = Arrangement.SpaceAround
                            ) {

                                Text(
                                    text = "你应该了解的中医药文化",
                                    fontSize = 20.sp,
                                    fontWeight = FontWeight.Bold
                                )

                                Text(
                                    text = "浙江中医药大学 焦阳",
                                    fontSize = 14.sp,
                                    color = Color.Gray
                                )
                            }

                        }


                    }


                }

            }
        }

//        标题栏

        Box(
            modifier = Modifier
                .fillMaxWidth()
                .padding(horizontal = 16.dp)
        ) {
            TitleWithMoreTab("名家讲堂", click = {

            })
        }

        LazyHorizontalGrid(modifier = Modifier
            .fillMaxWidth()
            .height(270.dp)
            .padding(horizontal = 16.dp),

            rows = GridCells.Fixed(2),
            verticalArrangement = Arrangement.spacedBy(12.dp),
            horizontalArrangement = Arrangement.spacedBy(12.dp),
            content = {

                items(courseList.size) { index: Int ->
                    WebViewCard(data = courseList[index], modifier = Modifier.clickable {

                        val intent=Intent().apply {
                            action=Intent.ACTION_VIEW
                            data= Uri.parse("http://m.zhiyuanzhongyi.com/")
                        }
                        context.startActivity(intent)
                    })
                }
            }
        )
        Spacer(modifier = Modifier.height(8.dp))
    }


}



